//
//  NSString+Extra.m
//  Oos4ios
//
//  Created by Pablo Gonzalez on 3/22/11.
//  Copyright 2011 11870.com. All rights reserved.
//

#import "NSStringUtils.h"


@implementation NSStringUtils

+ (NSString *) removePrefix:(NSString *) prefix 
                 fromString:(NSString *) string
{
    return [string stringByReplacingOccurrencesOfString:prefix withString:@""];
}

+ (NSString *) pluralOf: (NSString *) string
{
	if ([string hasSuffix:@"y"]) {
		return [string stringByReplacingOccurrencesOfString:@"y" withString:@"ies"];
	} else {
		return [string stringByAppendingString:@"s"];
	}
}

+ (BOOL) string: (NSString *) string 
       hasValue: (NSString *) value
{
	return [string rangeOfString:value].location != NSNotFound;
}

+ (NSArray /* NSString */ *) valuesFromString: (NSString *) string;
{
	return [string componentsSeparatedByString:@","];
}

+ (NSString *) stringWithValues: (NSArray *) values
{
	if (values && [values count] > 0) {
		return [values componentsJoinedByString: @","];
	} else {
		return nil;
	}

}

+ (NSString *) addValue: (NSString *) value toString: (NSString *) string
{
	if ([string length] == 0) {
		return value;
	}
	if (![self string: string hasValue:value]) {
        NSArray *values = [self valuesFromString:string];
		NSMutableArray *mutable = [NSMutableArray arrayWithArray: values];
		[mutable addObject: value];
		return [self stringWithValues:mutable];
	} else {
		return string;
	}
}

+ (NSString *) removeValue: (NSString *) value fromString:(NSString *)string
{
	if ([self string: string hasValue:value]) {
        NSArray *values = [self valuesFromString:string];
		NSMutableArray *mutable = [NSMutableArray arrayWithArray:values];
		[mutable removeObject: value];
		if ([mutable count] > 0) {
			return [self stringWithValues:mutable];
		} else {
			return nil;
		}
	} else {
		return string;
	}
}



@end
